<?php
class User {
    function checkUser($uid, $oauth_provider, $username){
        $connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
        $database = mysql_select_db(DB_DATABASE) or die(mysql_error());
        $select_sql = "SELECT u.`id`, u.`role`,s.`character`, s.`items`, s.`weak`, s.`sort_by`
                              FROM `users` u
                              LEFT JOIN `users_settings` s ON s.`user` = u.`id`
                              WHERE u.`oauth_uid` = '$uid' and u.`oauth_provider` = '$oauth_provider'";
        $query = mysql_query($select_sql) or die(mysql_error());
        $result = mysql_fetch_array($query);
        if (empty($result)) {
            $query = mysql_query("INSERT INTO `users` (oauth_provider, oauth_uid, username) VALUES ('$oauth_provider', $uid, '$username')") or die(mysql_error());
            $query = mysql_query('SELECT `id` from `users` WHERE oauth_provider = \''.$oauth_provider.'\' AND oauth_uid = \''.$uid.'\'');
            $result = mysql_fetch_array($query);
            $query = mysql_query('INSERT INTO `users_settings` SET `user` = \''.$result[0]['id'].'\'') or die(mysql_error());
            $query = mysql_query($select_sql);
            $result = mysql_fetch_array($query);
        }
        mysql_close($query);
        return $result;
    }
}
?>
